﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using MyFunction.dbFuncion;
using MyFunction.SQLiteHelper;

namespace dataDict
{
    /// <summary>
    ///  字典查找窗体
    /// </summary>
    public partial class FieldSearch : Form
    {
        /// <summary>
        ///  数据库名
        /// </summary>
       string DatabaseName;
        /// <summary>
        ///  联接字串
        /// </summary>
        string ConStr;
        /// <summary>
        ///  窗体 FieldSearch 构造函数
        /// </summary>
        /// <param name="database"></param>
        public FieldSearch ( string database )
        {
            InitializeComponent ( );
            DatabaseName = database;
          
        }
        #region 退出
        /// <summary>
        ///  按钮 -- 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripExit_Click ( object sender , EventArgs e )
        {
            Close ( );
        }
        #endregion

        #region 窗体载入事件
        /// <summary>
        ///  窗体载入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FieldSearch_Load ( object sender , EventArgs e )
        {
            ConStr = dBFunctions.ConnectionStringSQLite;
            BindDatabase ( );
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
       //     comboBoxDatabase.Text = DatabaseName;
        }
        #endregion



        #region  显示数据库名
        /// <summary>
        ///  方法：控件 comboBoxDatabase 绑定数据库名列表
        /// </summary>
        private void BindDatabase ( )
        {
            SqliteHelper sqliteHelper = new SqliteHelper ( ConStr );
            string cmdText = "select distinct database from u_systable order by upper(database) ";
            comboBoxDatabase.Items.Clear ( ); //清除原有数据
            if (sqliteHelper.Load ( cmdText ))
            {

                DataTable dt = sqliteHelper.DataSet.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    comboBoxDatabase.Items.Add ( dr[0] );
                }
                if (comboBoxDatabase.Items.Count > 0)
                {
                    //comboBoxDatabase.SelectedIndex = 0;
                    comboBoxDatabase.Text = DatabaseName;
                }
                

            }

        }
        #endregion

        #region 检索数据
        /// <summary>
        ///  按钮：按指定的条件检索数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonSearch_Click ( object sender , EventArgs e )
        {
            DatabaseName = comboBoxDatabase.Text.Trim ( );
            tableListTableAdapter.Fill ( dataSet1.TableList , DatabaseName );
            StringBuilder Filter = new StringBuilder ( );
            Filter.Append ( "1=1" );
            if (comboBoxSys_id.Text != "全部分类")
            {
                Filter.Append ( " and sys_id ='" + comboBoxSys_id.Text.Trim ( )+"'" );
            }
            #region 表名筛选
            if (!string.IsNullOrEmpty ( textBoxTableName.Text ))
            {
                if (comboBox1.Text == "包含")
                {
                    Filter.Append ( " and table_name like '%" + textBoxTableName.Text.Trim ( ) + "%'" );
                }
                else
                {
                    Filter.Append ( " and table_name = '" + textBoxTableName.Text.Trim ( ) + "'" );
                }
            }
            #endregion

            #region 字段筛选
            if (!string.IsNullOrEmpty ( textBoxFieldName.Text ))
            {
                if (comboBox2.Text == "包含")
                {
                    Filter.Append ( " and field_name like '%" + textBoxFieldName.Text.Trim ( ) + "%'" );
                }
                else
                {
                    Filter.Append ( " and field_name = '" + textBoxFieldName.Text.Trim ( ) + "'" );
                }

            }
            #endregion
            #region 字段描述筛选
            if (!string.IsNullOrEmpty ( textBoxFieldComment.Text ))
            {
                if (comboBox3.Text == "包含")
                {
                    Filter.Append ( " and field_desc like '%" + textBoxFieldComment.Text.Trim ( ) + "%'" );
                }
                else
                {
                    Filter.Append ( " and field_desc = '" + textBoxFieldComment.Text.Trim ( ) + "'" );
                }

            }
            #endregion
            DataTable dt = dataSet1.TableList;
            dt.DefaultView.RowFilter = Filter.ToString ( );
            tableListBindingSource.DataSource = dt.DefaultView;
            //向报表传递参数
            ReportParameter[] p = new ReportParameter[1];
            p[0] = new ReportParameter("Title", DatabaseName.Trim()+" 表结构");
            reportViewer1.LocalReport.SetParameters(p);
            reportViewer1.RefreshReport ( );
        }
        #endregion

        #region 子系统分类
        /// <summary>
        ///  方法：给控件 comboBoxSys_id 绑定 子系统分类数据
        /// </summary>
        /// <param name="database"></param>
        private void BindSysId ( string database )
        {
            SqliteHelper sqliteHelper = new SqliteHelper ( ConStr );
            string cmdText = "select distinct trim(sys_id) from u_systable where database ='" + database + "' order by upper(sys_id) ";
            comboBoxSys_id.Items.Clear ( ); //清除原的数据
            if (sqliteHelper.Load ( cmdText ))
            {
                comboBoxSys_id.Items.Add ( "全部分类" );
                DataTable dt = sqliteHelper.DataSet.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    comboBoxSys_id.Items.Add ( dr[0] );
                }
                if (comboBoxSys_id.Items.Count > 0)
                {
                    comboBoxSys_id.SelectedIndex = 0;
                }
            }
        }
        #endregion

        #region 选取子系统分类
        /// <summary>
        /// 事件：comboBoxDatabase值变化时，重新绑定子系统分类
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxDatabase_SelectedIndexChanged ( object sender , EventArgs e )
        {
            DatabaseName = comboBoxDatabase.Text;
            BindSysId ( DatabaseName );
        }
        #endregion

    }
}
